<?php

namespace Console\Controller;

use Console\Common\Library\Page;

/**
 * 信鸽控制器
 */
class PigeonController extends AuthController {

    private $settingLogic;
    private $pigeonLogic;

    public function _initialize() {
        parent::_initialize();
        $this->settingLogic = A('Console/Setting', 'Logic');
        $this->pigeonLogic = A('Console/Pigeon', 'Logic');
    }

    /**
     * 信鸽列表
     */
    public function lists() {
        $colorDic = $this->settingLogic->getColorDic();
        $eyeDic = $this->settingLogic->getEyeDic();
        $this->assign("colorDic", $colorDic);
        $this->assign("eyeDic", $eyeDic);
        $this->display();
    }

    /**
     * 获取父母列表
     */
    public function doSearchParentList() {
        $keyword = I('post.keyword');
        $sex = I('post.sex');

        $where = array(
            "sex" => array("in", $sex)
        );
        if (!empty($keyword)) {
            $where['_complex'] = array(
                "cid" => array('like', "%$keyword%"),
                "name" => array('like', "%$keyword%"),
                "_logic" => "or"
            );
        }

        $list = $this->pigeonLogic->lists(1, 20, $where);
        $this->assign("list", $list);
        $html = $this->fetch("doSearchParentList");
        $resData = array(
            "html" => $html
        );
        $this->ajaxReturn(static::$res->suc("成功", $resData));
    }

    /**
     * 获取信鸽列表
     */
    public function doGetPigeonList() {
        $where = I('post.');
        $page = $where["page"];
        $size = 15;
        unset($where["page"]);

        $likeDic = array(
            "cid" => 1,
            "name" => 1,
            "bloodlines" => 1,
            "raiser" => 1,
            "client_info" => 1
        );
        foreach ($where as $key => $val) {
            if (empty($val)) {
                unset($where[$key]);
            }

            if ($likeDic[$key]) {
                $where[$key] = array('like', "%$val%");
            }
        }

        if (!empty($where["birthday_begin"])) {
            $where["birthday"][] = array('egt', $where["birthday_begin"]);
            unset($where["birthday_begin"]);
        }
        if (!empty($where["birthday_end"])) {
            $where["birthday"][] = array('elt', $where["birthday_end"]);
            unset($where["birthday_end"]);
        }

        if (count($where["birthday"]) == 1) {
            $where["birthday"] = $where["birthday"][0];
        }

        $order = "id desc";
        $list = $this->pigeonLogic->lists($page, $size, $where, $order);
        $total = $this->pigeonLogic->total($where);

        $PageLib = new Page($total, $size);
        $pageHtml = $PageLib->show();
        $pageTotal = ceil($total / $size);

        //获取性别字典
        $sexDic = $this->pigeonLogic->getSexDic();
        $salesStatusDic = $this->pigeonLogic->getSalesStatusDic();

        $this->assign("total", $total);
        $this->assign("pageTotal", $pageTotal);
        $this->assign("pageHtml", $pageHtml);
        $this->assign("list", $list);
        $this->assign("sexDic", $sexDic);
        $this->assign("salesStatusDic", $salesStatusDic);
        $html = $this->fetch('doGetPigeonList');

        $res = array(
            "list" => $list,
            "html" => $html,
            "total" => $total,
            "pageTotal" => $pageTotal
        );
        $this->ajaxReturn(static::$res->suc("成功", $res));
    }

    /**
     * 获取信鸽提交表单
     */
    public function doGetAddPigeonForm() {
        $colorDic = $this->settingLogic->getColorDic();
        $eyeDic = $this->settingLogic->getEyeDic();
        $this->assign("colorDic", $colorDic);
        $this->assign("eyeDic", $eyeDic);

        $html = $this->fetch('doGetAddPigeonForm');
        $this->ajaxReturn(static::$res->suc("成功", $html));
    }

    /**
     * 添加信鸽
     */
    public function doAddPigeon() {
        $userInfo = $this->getUserInfo();
        $userId = $userInfo["id"];
        $post = I('post.');

        $res = $this->pigeonLogic->addPigeon($userId, $post);

        if (static::$res->isSuc($res)) {
            if (!empty($post['color']) && empty($post['color_id'])) {
                $this->settingLogic->addColor($userId, $post['color']);
            }

            if (!empty($post['eye']) && empty($post['eye_id'])) {
                $this->settingLogic->addEye($userId, $post['eye']);
            }
        }

        $this->ajaxReturn($res);
    }

    /**
     * 获取编辑表单
     */
    public function doGetEditPigeonForm() {
        $id = I('post.id');
        if (empty($id)) {
            $this->ajaxReturn(static::$res->err("参数异常"));
        }

        $where = array(
            "id" => $id
        );

        $info = $this->pigeonLogic->find($where);
        if (empty($info)) {
            $this->ajaxReturn(static::$res->err("未找到信鸽数据"));
        }

        $fatherInfo = array();
        if ($info["father_id"]) {
            $fatherWhere = array(
                "id" => $info["father_id"]
            );
            $fatherInfo = $this->pigeonLogic->find($fatherWhere);
        }

        $motherInfo = array();
        if ($info["mother_id"]) {
            $motherWhere = array(
                "id" => $info["mother_id"]
            );
            $motherInfo = $this->pigeonLogic->find($motherWhere);
        }

        //获取性别字典
        $sexDic = $this->pigeonLogic->getSexDic();
        //获取销售状态字典
        $salesStatusDic = $this->pigeonLogic->getSalesStatusDic();

        $colorDic = $this->settingLogic->getColorDic();
        $eyeDic = $this->settingLogic->getEyeDic();
        $this->assign("info", $info);

        $this->assign("fatherInfo", $fatherInfo);
        $this->assign("motherInfo", $motherInfo);

        $this->assign("sexDic", $sexDic);
        $this->assign("salesStatusDic", $salesStatusDic);
        $this->assign("colorDic", $colorDic);
        $this->assign("eyeDic", $eyeDic);

        $html = $this->fetch('doGetEditPigeonForm');
        $this->ajaxReturn(static::$res->suc("成功", $html));
    }

    /**
     * 编辑信鸽
     */
    public function doEditPigeon() {
        $userInfo = $this->getUserInfo();
        $userId = $userInfo["id"];
        $post = I('post.');
        $pigeonId = $post["id"];
        unset($post["id"]);

        if (empty($pigeonId)) {
            $this->ajaxReturn(static::$res->err("信鸽ID不能为空"));
        }

        $res = $this->pigeonLogic->editPigeon($userId, $pigeonId, $post);

        if (static::$res->isSuc($res)) {
            if (!empty($post['color']) && empty($post['color_id'])) {
                $this->settingLogic->addColor($userId, $post['color']);
            }

            if (!empty($post['eye']) && empty($post['eye_id'])) {
                $this->settingLogic->addEye($userId, $post['eye']);
            }
        }

        $this->ajaxReturn($res);
    }

    /**
     * 删除信鸽
     */
    public function doDelPigeon() {
        $pigeonId = I('post.id');
        if (empty($pigeonId)) {
            $this->ajaxReturn(static::$res->err("信鸽ID不能为空"));
        }

        $userInfo = $this->getUserInfo();
        $userId = $userInfo["id"];

        $res = $this->pigeonLogic->delPigeon($userId, $pigeonId);
        $this->ajaxReturn($res);
    }

    /**
     * 获取信鸽明细
     */
    public function doGetDetailPigeon() {
        $id = I('post.id');
        if (empty($id)) {
            $this->ajaxReturn(static::$res->err("参数异常"));
        }

        $where = array(
            "id" => $id
        );

        $info = $this->pigeonLogic->find($where);
        if (empty($info)) {
            $this->ajaxReturn(static::$res->err("未找到信鸽数据"));
        }

        if (!empty($info['bloodlines_cont'])) {
            $info['bloodlines_cont'] = str_replace(PHP_EOL, '<br/>', $info['bloodlines_cont']);
        }

        if (!empty($info['remark'])) {
            $info['remark'] = str_replace(PHP_EOL, '<br/>', $info['remark']);
        }

        $fatherInfo = array();
        if ($info["father_id"]) {
            $fatherWhere = array(
                "id" => $info["father_id"]
            );
            $fatherInfo = $this->pigeonLogic->find($fatherWhere);
        }

        $motherInfo = array();
        if ($info["mother_id"]) {
            $motherWhere = array(
                "id" => $info["mother_id"]
            );
            $motherInfo = $this->pigeonLogic->find($motherWhere);
        }

        //获取性别字典
        $sexDic = $this->pigeonLogic->getSexDic();
        //获取销售状态字典
        $salesStatusDic = $this->pigeonLogic->getSalesStatusDic();

        $this->assign("info", $info);

        $this->assign("fatherInfo", $fatherInfo);
        $this->assign("motherInfo", $motherInfo);

        $this->assign("sexDic", $sexDic);
        $this->assign("salesStatusDic", $salesStatusDic);

        $html = $this->fetch('doGetDetailPigeon');
        $this->ajaxReturn(static::$res->suc("成功", $html));
    }

    /**
     * 生成族谱图
     */
    public function doGetPRImg() {
        $id = I('id');
        $type = I('type');
        if (empty($id)) {
            $this->ajaxReturn(static::$res->err("参数异常"));
        }

        $PRLogic = A('Console/PR', 'Logic');
        $initRes = $PRLogic->init($id, $type);
        if (static::$res->isErr($initRes)) {
            $this->ajaxReturn($initRes);
        }

        $res = $PRLogic->draw();
        $this->ajaxReturn($res);
    }

}
